
package Interface;

import Main.*;
import Process.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

public class Interface extends JFrame implements ActionListener {
    //ShowCanvas canvas;

    JFrame frame;
    public static int click = 1;
    MainWind main;
    Icon ic = new ImageIcon("images/PL.jpg");
    Icon ic1 = new ImageIcon("images/PP.jpg");
    Border blackline = BorderFactory.createLineBorder(Color.black);
    JLabel t[] = new JLabel[13];
    JLabel o[] = new JLabel[13];
    JLabel p[] = new JLabel[13];
    JLabel me[] = new JLabel[13];
    String tapbai[] = new String[13];
    Hearts control;
    Container container;
    public JPanel panel_;
    public int x1[] = new int[13];
    public int x2[] = new int[13];
    public int x3[] = new int[13];
    public int x4[] = new int[13];
    public int y1[] = new int[13];
    public int y2[] = new int[13];
    public int y3[] = new int[13];
    public int y4[] = new int[13];

    public Interface(Hearts control) {
        this.control = control;
        DrawServerInterface();
        for (int i = 0; i < 13; i++) {
            x1[i] = 580;
            x2[i] = 580;
            x3[i] = 580;
            x4[i] = 580;
            y1[i] = 310;
            y2[i] = 310;
            y3[i] = 310;
            y4[i] = 310;
        }
        this.control.SP= new ServerProcess(control);
    }

    private void DrawServerInterface() {

        // Create the menu bar
        JMenuBar menuBar = new JMenuBar();

        // Create a menu
        JMenu Game = new JMenu("Game");
        menuBar.add(Game);

        JMenu Help = new JMenu("Help");
        menuBar.add(Help);

        // Create a menu item
        JMenuItem Start = new JMenuItem("Start");
        Start.setActionCommand("Start");
        Start.addActionListener(new StartAnimationAction());
        Game.add(Start);

        JMenuItem Exit = new JMenuItem("Exit");
        Exit.setActionCommand("Exit");
        Start.addActionListener(this);
        Game.add(Exit);

        JMenuItem HowtoPlay = new JMenuItem("How to play");
        HowtoPlay.setActionCommand("How");
        Start.addActionListener(this);
        Help.add(HowtoPlay);

        JMenuItem About = new JMenuItem("About Hearts");
        About.setActionCommand("About");
        Start.addActionListener(this);
        Help.add(About);

        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        panel_ = new MyPanel();
        panel_.setBounds(0, 0, 1278, 770);
        frame.getContentPane().add(panel_);

        frame.setSize(1278, 770);
        frame.setVisible(true);
        frame.setJMenuBar(menuBar);

        
    }

    class StartAnimationAction implements ActionListener, Runnable {

        public void actionPerformed(ActionEvent e) {

            Thread thread = new Thread(this);
            thread.start();

        }

        public void run() {
            
            for (int j = 0; j < 13; j++) {
                for (int i = 0; i < 275; i++) {
                    x1[j] += 2;
                    x2[j] -= 2;
                    y3[j]--;
                    y4[j]++;
                    panel_.repaint();
                    try {
                        Thread.sleep(1);
                    } catch (Exception ex) {
                    }
                }
                
            }

            for (int j = -6; j < 7; j++) {
                for (int i = 0; i < 37; i++) {
                    y1[j + 6] = y1[j + 6] + j * 1 - 1;
                    y2[j + 6] = y2[j + 6] - j * 1 - 1;
                    x3[j + 6] = x3[j + 6] + j * 1;
                    x4[j + 6] = x4[j + 6] - j * 2;
                    panel_.repaint();
                    try {
                        Thread.sleep(1);
                    } catch (Exception ex) {
                    }
                }
            }
        }
    }

    class MyPanel extends JPanel {

        public void paintComponent(Graphics g) {

            g.setColor(Color.white);
            g.fillRect(0, 0, this.getWidth(), this.getHeight());

            Icon CardStatic = new ImageIcon("images/ND.jpg");
            JLabel LabelCardStatic = new JLabel(CardStatic);
            LabelCardStatic.setBounds(550, 300, 120, 140);

            Icon Card1[], Card2[], Card3[], Card4[];
            Card1 = new Icon[13];
            Card2 = new Icon[13];
            Card3 = new Icon[13];
            Card4 = new Icon[13];

            JLabel LabelCard1[], LabelCard2[], LabelCard3[], LabelCard4[];
            LabelCard1 = new JLabel[13];
            LabelCard2 = new JLabel[13];
            LabelCard3 = new JLabel[13];
            LabelCard4 = new JLabel[13];

            for (int i = 0; i < 13; i++) {
                Card1[i] = new ImageIcon("images/ND.jpg");
                LabelCard1[i] = new JLabel(Card1[i]);
                LabelCard1[i].setBounds(x1[i], y1[i], 90, 110);


                Card2[i] = new ImageIcon("images/ND.jpg");
                LabelCard2[i] = new JLabel(Card2[i]);
                LabelCard2[i].setBounds(x2[i], y2[i], 90, 110);

                Card3[i] = new ImageIcon("images/ND.jpg");
                LabelCard3[i] = new JLabel(Card3[i]);
                LabelCard3[i].setBounds(x3[i], y3[i], 90, 110);

                Card4[i] = new ImageIcon("images/ND.jpg");
                LabelCard4[i] = new JLabel(Card4[i]);
                LabelCard4[i].setBounds(x4[i], y4[i], 90, 110);
            }

            panel_.removeAll();
            for (int i = 0; i < 13; i++) {
                panel_.add(LabelCard1[i]);
                panel_.add(LabelCard2[i]);
                panel_.add(LabelCard3[i]);
                panel_.add(LabelCard4[i]);
            }

        }

    }

    public void actionPerformed(ActionEvent e) {

        String strActionCommand = e.getActionCommand();

        if (strActionCommand.equals("Start")) {
           
        }
        if (strActionCommand.equals("Exit")) {
            
        }
    }
}
